<?php
include_once "../../../../common.php";
if(!isset($_SESSION['klanten']['id']))
{
	$query = "	SELECT 
					W.ID 
				FROM Webshops AS W
					LEFT JOIN WebshopContactpersonen AS WCP ON WCP.WebshopID = W.ID 
													AND WCP.Email != '' 
													AND WCP.Email IS NOT NULL
				WHERE 1 ";
	// filters
	if(isset($_SESSION['klanten']['filter']['status']) && !empty($_SESSION['klanten']['filter']['status']) && is_numeric($_SESSION['klanten']['filter']['status']))
		$query .= " AND W.Status = '".str_replace("'","",$_SESSION['klanten']['filter']['status'])."' ";
	
	if(isset($_SESSION['klanten']['filter']['email']) && !empty($_SESSION['klanten']['filter']['email']) && $_SESSION['klanten']['filter']['email'] == 'geen')
		$query .= " AND WCP.ID IS NULL";
	elseif(isset($_SESSION['klanten']['filter']['email']) && !empty($_SESSION['klanten']['filter']['email']) && $_SESSION['klanten']['filter']['email'] == 'wel')
		$query .= " AND WCP.ID IS NOT NULL";
			
	
	$query .= " ORDER BY W.ID ASC LIMIT 1";
	$result = $_MySQL->runQuery($query);
	$row = mysql_fetch_assoc($result);
	$_SESSION['klanten']['id'] = $row['ID'];
}
else
{
	$query = "	SELECT 
					W.ID 
				FROM Webshops AS W 
					LEFT JOIN WebshopContactpersonen AS WCP ON WCP.WebshopID = W.ID 
													AND WCP.Email != '' 
													AND WCP.Email IS NOT NULL
				WHERE W.ID < '".str_replace("'","",$_SESSION['klanten']['id'])."'";
	// filters
	if(isset($_SESSION['klanten']['filter']['status']) && !empty($_SESSION['klanten']['filter']['status']) && is_numeric($_SESSION['klanten']['filter']['status']))
		$query .= " AND W.Status = '".str_replace("'","",$_SESSION['klanten']['filter']['status'])."' ";
	
	if(isset($_SESSION['klanten']['filter']['email']) && !empty($_SESSION['klanten']['filter']['email']) && $_SESSION['klanten']['filter']['email'] == 'geen')
		$query .= " AND WCP.ID IS NULL";
	elseif(isset($_SESSION['klanten']['filter']['email']) && !empty($_SESSION['klanten']['filter']['email']) && $_SESSION['klanten']['filter']['email'] == 'wel')
		$query .= " AND WCP.ID IS NOT NULL";
			
	
	$query .= " ORDER BY W.ID DESC LIMIT 1";
	$result = $_MySQL->runQuery($query);
	$row = mysql_fetch_assoc($result);
	$_SESSION['klanten']['id'] = $row['ID'];
}
echo $_SESSION['klanten']['id'];